package com.maniu.webrtcmaniua;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import java.io.IOException;

public class LocalSurfaceView extends SurfaceView implements SurfaceHolder.Callback,
        Camera.PreviewCallback {
    EncodecPushLiveH264 encodecPushLiveH264;
    private Camera mCamera;
    private Camera.Size size;


    public LocalSurfaceView(Context context) {
        super(context);
    }

    public LocalSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
    }

    //
    public void startCaptrue(SocketLive.SocketCallback socketCallback) {
        encodecPushLiveH264 = new EncodecPushLiveH264(socketCallback, size.width, size.height);
        encodecPushLiveH264.startLive();
    }

    //    data      NV21   ----》NV12       数据横向
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if (encodecPushLiveH264 != null) {
            encodecPushLiveH264.encodeFrame(data);
        }
        if (mCamera != null) {
            mCamera.addCallbackBuffer(data);
        }

    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        startPreview();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
        closeCamera();
    }

    public void startPreview() {


        try {
            mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
//        流程
            Camera.Parameters parameters = mCamera.getParameters();
//尺寸
            size = parameters.getPreviewSize();
            mCamera.setPreviewDisplay(getHolder());
//            横着
            mCamera.setDisplayOrientation(90);
//            width  heith
            byte[] buffer = new byte[size.width * size.height * 3 / 2];
            mCamera.addCallbackBuffer(buffer);
            mCamera.setPreviewCallbackWithBuffer(this);
//            输出数据怎么办
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void closeCamera() {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

}
